package com.example.myapplication;

import android.Manifest;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.example.myapplication.CustomView.FragmentConteact;
import com.example.myapplication.CustomView.MyView;
import com.example.myapplication.fragment.BlankFragment;
import com.example.myapplication.fragment.BlankFragment2;
import com.example.myapplication.fragment.BlankFragment3;
import com.example.myapplication.fragment.BlankFragment4;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements FragmentConteact {

    private MyView myView;
    private Fragment mCurrFragment;
    private Map<MainType,Fragment> mCacheFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        requestPermissions(new String[]{
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CAMERA

        }, 200);
    }

    private void initView() {
        myView = (MyView) findViewById(R.id.frag_view);
    }

    private void initData() {
        mCacheFragment = new HashMap<>();

        myView.addTextandColor("首页").addTextandColor("同城").addImage(R.mipmap.xiangji_hei).addTextandColor("消息").addTextandColor("我的")
                .listener(new MyView.ClickBarListener() {
                    @Override
                    public void onChickItem(int position) {

                        switch (position) {
                            case 0:
                                showFragment(MainType.HOME);

                                break;
                            case 1:
                                showFragment(MainType.CITY);

                                break;
                            case 3:
                                showFragment(MainType.MSG);

                                break;
                            case 4:
                                showFragment(MainType.MINE);

                                break;
                        }
                    }
                })
                .show();
        //第一次直接加载第一个页面
        showFragment(MainType.HOME);
    }

    @Override
    public void showFragment(MainType type) {
        if (mCurrFragment == null){
            getSupportFragmentManager().beginTransaction().add(R.id.main_lin,findFragment(type)).commit();
        }else{
            if (mCacheFragment.containsKey(type)){
                getSupportFragmentManager().beginTransaction().hide(mCurrFragment).show(findFragment(type)).commit();
            }else{
                getSupportFragmentManager().beginTransaction().hide(mCurrFragment).add(R.id.main_lin,findFragment(type)).commit();
            }

        }
        mCurrFragment = findFragment(type);

    }
    public Fragment findFragment(MainType type){
        switch (type){
            case HOME:
                if (!mCacheFragment.containsKey(type)){
                  mCacheFragment.put(type,new BlankFragment());
                }
                return mCacheFragment.get(type);
            case CITY:
                if (!mCacheFragment.containsKey(type)){
                    mCacheFragment.put(type,new BlankFragment2());
                }
                return mCacheFragment.get(type);
            case MSG:
                if (!mCacheFragment.containsKey(type)){
                    mCacheFragment.put(type,new BlankFragment3());
                }
                return mCacheFragment.get(type);

            default:
            case MINE:
                if (!mCacheFragment.containsKey(type)){
                    mCacheFragment.put(type,new BlankFragment4());
                }
                return mCacheFragment.get(type);

        }
    }



    @Override
    public void showMenu() {

    }

    @Override
    public void showStyle(boolean ischecked) {

    }
}
